import {
  setTimeout
} from 'core-js'
import Vue from 'vue'
import Vuex from 'vuex'
import user from "./user/index.js"
import {
  modifyLoginShow,
  showToast
} from "./const";

Vue.use(Vuex)
let timer = null;
export default new Vuex.Store({
  state: {
    loginshow: false,
    // 决定toastshow显示
    toastshow: false,
    // 弹出的信息提示
    toastmsg: "默认提示",
    // 存储模块化的信息
    toastIcon: "error",
    // 购物车状态管理
    shopshow: localStorage.getItem("token") ? true : false
  },
  getters: {},
  mutations: {
    changeloginshow(state, payload) {
      state.loginshow = payload
    },
    // 弹框显示，传值
    changetoastshow(state, payload) {
      state.toastshow = true;
      state.toastmsg = payload.toastmsg;
      state.toastIcon = payload.toastIcon;
    },
    // 
    changetoasts(state) {
      state.toastshow = false;
    },
    // 管理购物车状态
    changeshopshow(state, payload) {
      state.shopshow = payload;
    }
  },
  actions: {
    // 防抖和节流
    // 防抖就是以最后一次为准,前面无论点多少次
    // 节流是一段时间内，只触发一次，无论点多少次
    asynctoasts(context, payload) {
      // 每次弹出，我们都希望过一会就消失，所以直接在这个地方弹出，再延迟消失，就不用每次手动控制消失
      clearTimeout(timer);
      context.commit("changetoastshow", payload)
      timer = setTimeout(() => {
        context.commit("changetoasts")
      }, 2000);

    }
  },
  modules: {
    user,
  }
})